#include "hi_appcomm.h"
#include "ConfigSubOps.h"
#include "DevManager.h"

#if 1
#define TIMEZONE_KEY_ENABLE       "bEnable"
#define TIMEZONE_KEY_STARTTIME    "startTime"
#define TIMEZONE_KEY_ENDTIME      "endTime"
#define TIMEZONE_KEY_ZONESEC      "zoneSec"
#define TIMEZONE_KEY_RESERVE      "reserved"

int ConfigSubSetTimeZone(SYS_TIMEZONE_INFO_S *pTimeZone)
{
	int nret = -1;
	char  strTimeZone[256] = {0};
	if(NULL == pTimeZone)  return -1;

	snprintf(strTimeZone, sizeof(strTimeZone),
		     "#%s:%d#%s:%d#%s:%d#%s:%d#%s:%s#",
		     TIMEZONE_KEY_ENABLE, pTimeZone->stDayLightTime.bEnable,
		     TIMEZONE_KEY_STARTTIME, pTimeZone->stDayLightTime.startTime,
		     TIMEZONE_KEY_ENDTIME, pTimeZone->stDayLightTime.endTime,
		     TIMEZONE_KEY_ZONESEC, pTimeZone->zoneSec,
		     TIMEZONE_KEY_RESERVE, pTimeZone->reserved
		     );

	//LOGE("DevSetTimeZone strTimeZone=%s\n",strTimeZone);
	if(0 > DevSetTimeZone(strTimeZone)){
		LOGE("DevSetTimeZone fail\n");
		return -1;
	}
	return 0;
}

int ConfigSubGetTimeZone(SYS_TIMEZONE_INFO_S *pTimeZone)
{
	char  strformat[256]   = {0};
	char  strTimeZone[256] = {0};
	
	if(NULL == pTimeZone)  return -1;

	memset(pTimeZone, 0, sizeof(SYS_TIMEZONE_INFO_S));
	if(0 > DevGetTimeZone(strTimeZone)){
		LOGE("DevGetTimeZone fail, so set default timezone\n");
		ConfigSubSetTimeZoneDefault();
		return -1;
	}
	//LOGE("DevGetTimeZone strTimeZone=%s\n",strTimeZone);

	//need sync get
	sscanf(strTimeZone, "#bEnable:%d#startTime:%d#endTime:%d#zoneSec:%d#reserved:%s", 
		   &pTimeZone->stDayLightTime.bEnable,
		   &pTimeZone->stDayLightTime.startTime,
		   &pTimeZone->stDayLightTime.endTime,
		   &pTimeZone->zoneSec,
		   pTimeZone->reserved);
	
	return 0;
}

int ConfigSubSetTimeZoneDefault(void)
{
	SYS_TIMEZONE_INFO_S  TimeZone;

	memset(&TimeZone, 0, sizeof(TimeZone));
	TimeZone.zoneSec = 8 * 3600;
	
	return ConfigSubSetTimeZone(&TimeZone);
}

#else
#include "json.h"

int ConfigSubSetTimeZone(SYS_TIMEZONE_INFO_S *pTimeZone)
{
	int  nret = -1;
	struct json_object *json_total       = NULL;
	struct json_object *stDayLightTime   = NULL;
	
	if(NULL == pTimeZone)  return -1;
	nret = access(CFG_TIMEZONE_FILE_NAME, F_OK);
	if (0 == nret){
		unlink(CFG_TIMEZONE_FILE_NAME); 
	}

	printf("ConfigSubSetTimeZone===================>>>>>>>>>\n");
	//json_total 
	json_total = json_object_new_object();
	if(NULL == json_total){
		printf("new json_total fail\n");
		return -1;
	}

	//zoneSec
	json_object_object_add(json_total,"zoneSec",json_object_new_int(pTimeZone->zoneSec));

	//DayTimeInfo
	stDayLightTime = json_object_new_object();
	if(NULL == stDayLightTime){
		printf("new DayTimeInfo fail\n");
		return -1;
	}
	//stDayLightTime.bEnable
	json_object_object_add(stDayLightTime,"bEnable",json_object_new_int(pTimeZone->stDayLightTime.bEnable));
	//stDayLightTime.startTime
	json_object_object_add(stDayLightTime,"startTime",json_object_new_int(pTimeZone->stDayLightTime.startTime));
	//stDayLightTime.endTime
	json_object_object_add(stDayLightTime,"endTime",json_object_new_int(pTimeZone->stDayLightTime.endTime));
	//stDayLightTime
	json_object_object_add(json_total,"stDayLightTime",stDayLightTime);
	
	//reserved
	json_object_object_add(json_total,"ExtData",json_object_new_string(pTimeZone->reserved));

	//write file
	//printf("%s\n",json_object_to_json_string(json_total));
	json_object_to_file(CFG_TIMEZONE_FILE_NAME, json_total);
	//json_object_put(stDayLightTime);
	json_object_put(json_total);
	return 0;
}

int ConfigSubGetTimeZone(SYS_TIMEZONE_INFO_S *pTimeZone)
{
	struct json_object *json_total       = NULL;
	struct json_object *json_tmp         = NULL;
	struct json_object *stDayLightTime   = NULL;


	if(NULL == pTimeZone)  return -1;
	json_total = json_object_from_file(CFG_TIMEZONE_FILE_NAME);
	if(NULL == json_total){
		printf("json read %s fail\n", CFG_TIMEZONE_FILE_NAME);
		return -1;
	}
	printf("%s\n",json_object_to_json_string(json_total));
		
	//zoneSec
    json_tmp = json_object_object_get(json_total, "zoneSec");
	if(NULL == json_tmp){
		printf("get zoneSec fail\n");
		return -1;
	}else{
		pTimeZone->zoneSec = json_object_get_int(json_tmp);
		//printf("zoneSec=%d\n",pTimeZone->zoneSec);
	}
	
	//reserved
	json_tmp = json_object_object_get(json_total, "reserved");
	if(NULL == json_tmp){
		printf("get reserved fail\n");
		return -1;
	}else{
		strcpy(pTimeZone->reserved, json_object_get_string(json_tmp));
		//printf("reserved=%s\n",pTimeZone->reserved);
	}
	
	//stDayLightTime        
	stDayLightTime = json_object_object_get(json_total,"stDayLightTime");
	if(NULL == stDayLightTime){
		printf("get stDayLightTime fail\n");
		return -1;
	}
	//stDayLightTime.startTime
	json_tmp = json_object_object_get(stDayLightTime,"startTime");
	if(NULL == json_tmp){
		printf("get StartTime fail\n");
		return -1;
	}else{
		pTimeZone->stDayLightTime.startTime = json_object_get_int(json_tmp);
		//printf("StartTime=%d\n", pTimeZone->stDayLightTime.startTime);
	}
	
	//stDayLightTime.endTime
	json_tmp = json_object_object_get(stDayLightTime,"endTime");
	if(NULL == json_tmp){
		printf("get EndTime fail\n");
		return -1;
	}else{
		pTimeZone->stDayLightTime.endTime = json_object_get_int(json_tmp);
		//printf("EndTime=%d\n", pTimeZone->stDayLightTime.endTime);
	}
	
	//stDayLightTime.bEnable
	json_tmp = json_object_object_get(stDayLightTime,"bEnable");
	if(NULL == json_tmp){
		printf("get Enable fail\n");
		return -1;
	}else{
		pTimeZone->stDayLightTime.bEnable = json_object_get_int(json_tmp);
		//printf("Enable=%d\n", pTimeZone->stDayLightTime.bEnable);
	}
	
	//free
	//json_object_put(json_DayTimeInfo);
	json_object_put(json_total);
	return 0;
}
#endif
